package com.leyou.page.mq;

import com.leyou.page.service.PageService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.leyou.common.constants.MQConstants.Exchange.ITEM_EXCHANGE_NAME;
import static com.leyou.common.constants.MQConstants.Queue.PAGE_ITEM_DOWN;
import static com.leyou.common.constants.MQConstants.Queue.PAGE_ITEM_UP;
import static com.leyou.common.constants.MQConstants.RoutingKey.ITEM_DOWN_KEY;
import static com.leyou.common.constants.MQConstants.RoutingKey.ITEM_UP_KEY;


@Component
public class ItemListener {

    @Autowired
    private PageService pageService;

    //1.上架: 创建静态页
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = PAGE_ITEM_UP, durable = "true"),   //定义队列
            exchange = @Exchange(   //定义交换机
                    value = ITEM_EXCHANGE_NAME,
//                    ignoreDeclarationExceptions = "true",   //忽略声明队列异常(用默认值 false)
                    type = ExchangeTypes.TOPIC
            ),
            key = ITEM_UP_KEY  //绑定routingkey
    ))
    public void itemUp(Long spuId){
        if (spuId != null){
            pageService.createStaticHtml(spuId);
        }
    }


    //2.下架: 删除静态页
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = PAGE_ITEM_DOWN, durable = "true"),   //定义队列
            exchange = @Exchange(   //定义交换机
                    value = ITEM_EXCHANGE_NAME,
//                    ignoreDeclarationExceptions = "true",   //忽略声明队列异常(用默认值 false)
                    type = ExchangeTypes.TOPIC
            ),
            key = ITEM_DOWN_KEY  //绑定routingkey
    ))
    public void itemDown(Long spuId){
        if (spuId != null){
            pageService.deleteStaticHtml(spuId);
        }
    }

}
